package com.example.foodisimportant.StudentViewActivity.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.foodisimportant.Adapter.MessageAdapter;
import com.example.foodisimportant.Bean.MessageEntity;
import com.example.foodisimportant.Canteens.MoreCommentsActivity;
import com.example.foodisimportant.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class MessageFragment extends Fragment {
    View messageLayout;
    private ArrayList<MessageEntity> messageEntityArrayList =new ArrayList<MessageEntity>();
    private RecyclerView mMessageRecyclerView;
    private MessageAdapter mMessageRecyclerAdapter;
    Calendar c = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
    String strDate = sdf.format(c.getTime());

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        messageLayout=inflater.inflate(R.layout.fragment_messages,container,false);
        initData();
        initRecyclerView();
        return messageLayout;
    }

    /**
     * 模拟数据
     */
    private void initData(){
        for(int i=0;i<10;i++)
        {
            MessageEntity messageEntity=new MessageEntity();
            messageEntity.setName("元和西饼");
            messageEntity.setPreview("用餐提醒");
            messageEntity.setTime(strDate);
            messageEntityArrayList.add(messageEntity);
        }
    }

    /**
     * 对recycleview进行设置
     */
    private void initRecyclerView(){
        //获取RecyclerView
        mMessageRecyclerView =messageLayout.findViewById(R.id.rv_comment);
        //创建adapter
        mMessageRecyclerAdapter = new MessageAdapter(getActivity(),messageEntityArrayList);
        //给RecyclerView设置adapter
        mMessageRecyclerView.setAdapter(mMessageRecyclerAdapter);
        //设置layoutManager,可以设置显示效果，是线性布局、grid布局，还是瀑布流布局
        //参数是：上下文、列表方向（横向还是纵向）、是否倒叙
        mMessageRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));
        //设置item的分割线
        mMessageRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        //RecyclerView中没有item的监听事件，需要自己在适配器中写一个监听事件的接口。参数根据自定义
        mMessageRecyclerAdapter.setOnItemClickListener(new MessageAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, MessageEntity data) {
                Intent intent=new Intent(getActivity(), MoreCommentsActivity.class);
                startActivity(intent);
            }
        });
    }
}
